/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

/**
 * The administrative program for NYMRL
 * 
 * import (restore)
 * export (backup)
 * report on data
 * export graph of network
 * new local
 * config
 * 	- set the location of the databases
 * clone
 * sync
 * 
 * */

#include "admin_nymrl.h"

char * graph_file_name = "nymrl_nod_graph.txt";

const char * help_message = "'h' or 'help'\n\t\thelp message (this message)"
	"\n\t'q' or 'quit'\t\t\t"
	"\n\t\texit program"
	"\n\t'v'"
	"\n\t\tversion info"
	"\n\t'adminurl (url)'"
	"\n\t\tset the host from which to allow admin messages\n This MUST be done from command line."
	"\n\t'setup  (reset=0|1) (/path/to/dbs) '\t\t\t"
	"\n\t\tbuild tables and db at [/path/to/dbs]"
	
	//set a self url rel=>url since  rel 1--1 url
	
	//list self url
	
	//remove self url

	
	"\n ----- TODO ----- \t\t\t"
	"\n\t'update'\t\t\t"
	"\n\t\tassuming this exucutable has been updated, run update"
	"\n\t'backup' \t\t\t"
	"\n\t\thot copy backup"
	"\n\t'clonefrom'\t\t\t"
	"\n\t\tclone from"
	"\n\t'cloneto'\t\t\t"
	"\n\t\tclone to "
	"\n\t'stats'\t\t\t"
	"\n\t\tstats "
	"\n\t'test'\t\t\t"
		"\n\t\trun unit tests "
	"\n\t'bench'\t\t\t"
		"\n\t\trun bench mark tests "
	"\n\t'sync'\t\t\t"
	"\n\t\tsync from to "
		"\n";

int nymrl_admin_do_setup(){
	char input = ' ';
	int drop_table = 0;
	printf("Should the databases be truncated?");
    input = getc(stdin);
	
	if (input == 'y'){
		printf("you pressed %c - ARE YOU SURE?",input);
		//newline
		input = getc(stdin);
	    input = getc(stdin);
		if (input == 'y'){
			printf("creating and truncating sqlite databases");
			//do backups
			drop_table = 1;
		}
	}
	return nyml_setup_self(drop_table);
} 

/**
 * 
 * */
int nymrl_admin_set_admin(char * url){
	NYMRL_ADMIN_PRINT_MSG("Set the master admin URL to %s?",url);
	char input = getc(stdin);
		
	if (input == 'y'){
	 	nyml_set_admin_url(url);
		NYMRL_ADMIN_PRINT_MSG("set admin server to \"%s\" ",url);
		ny_log_notice("set admin server to \"%s\" ",url);
	}
	input = getc(stdin);
	return 0;
}

void nymrl_admin_main_command(char * command){
	//tokenize and grab first token
	const char * delim = " ";
	char * tokenized_input = strtok( command, delim );
   
//	   while( tokenized_input != NULL ) {
//		   NYMRL_ADMIN_PRINT_MSG("tokenized_input: %s",tokenized_input);
//		   tokenized_input = strtok( NULL, delim );
//	   } 
	
	if ((strcmp(tokenized_input,"help") ==0) || (strcmp(tokenized_input,"h") ==0)){
		NYMRL_ADMIN_PRINT_MSG(help_message);
	} else if ((strcmp(tokenized_input,"help") ==0) || (strcmp(tokenized_input,"h") ==0)){
		NYMRL_ADMIN_PRINT_MSG(help_message);
	} else if ((strcmp(tokenized_input,"v") ==0) ){
		NYMRL_ADMIN_PRINT_MSG("Version %s.",NYMRL_ADMIN_VERSION );
	} else if ((strcmp(tokenized_input,"help") ==0) || (strcmp(tokenized_input,"h") ==0)){
		NYMRL_ADMIN_PRINT_MSG(help_message);
	} else if ((strcmp(tokenized_input,"help") ==0) || (strcmp(tokenized_input,"h") ==0)){
		NYMRL_ADMIN_PRINT_MSG(help_message);
	} else if ((strcmp(tokenized_input,"help") ==0) || (strcmp(tokenized_input,"h") ==0)){
		NYMRL_ADMIN_PRINT_MSG(help_message);
	} else if ((strcmp(tokenized_input,"adminurl") ==0)){
		tokenized_input = strtok( NULL, delim );
		nymrl_admin_set_admin(tokenized_input);
	} else if ((strcmp(tokenized_input,"setup") ==0)){
		nymrl_admin_do_setup();  
	}   else {
		NYMRL_ADMIN_PRINT_MSG("'%s' is not a valid command, or is not implemented.",tokenized_input);
	}
}

int main(){
	char user = ' ';
	//char previous = ' ';
	char command_buff[128];
	command_buff[0] = '\0';
	char * command_buff_p = command_buff;
	//welcome message
	NYMRL_ADMIN_PRINT_SPACER;
	NYMRL_ADMIN_PRINT_MSG("Welcome to the NYMRL admin tool.");
	NYMRL_ADMIN_PRINT_MSG("(c) 2007 Jonathan Hendler");
	NYMRL_ADMIN_PRINT_MSG("All rights reserved.");
	NYMRL_ADMIN_PRINT_MSG(" ");
	
	NYMRL_ADMIN_PRINT_MSG("This is version %s",NYMRL_ADMIN_VERSION);
	NYMRL_ADMIN_PRINT_SPACER;
	NYMRL_ADMIN_PRINT_MSG("Awaiting input. Press 'h' for help.\n");
	
	//get args, pass to command function 
	//if args empty, enter interactive mode
	
	
	printf("\ncmd> ");
	while(TRUE){
		user = getchar();
		if ((user == '\n') && (strlen(command_buff)>0)){
			*command_buff_p = '\0';
			
			if ((strcmp(command_buff,"q") == 0)||(strcmp(command_buff,"quit") == 0)){
				break;//break out of the loop
			} 
			nymrl_admin_main_command(command_buff);
			//reset the buffer
			command_buff_p = command_buff;
			printf("\ncmd> ");
		} else if ((user == '\n')){
			//reset the buffer
			command_buff_p = command_buff;
			printf("cmd> ");
		} else {
			*command_buff_p = user;
			command_buff_p++;
		}
	}  	
	
	NYMRL_ADMIN_PRINT_MSG("Exiting admin. Goodbye.\n\n");
	return 0;
}